home *** CD-ROM | disk | FTP | other *** search
/ Network CD 1 / Network CD.iso / tbag / 1-10 / tb3 / doc-files / utilities-doc / screen-saver.doc < prev    next >
Text File  |  1985-11-25  |  9KB  |  362 lines

  1. #include <exec/types.h>
  2. #include <intuition/intuition.h>
  3. #include <graphics/gfxbase.h>
  4. #include <exec/memory.h>
  5. #include <exec/execbase.h>
  6.  
  7. /*
  8. **    s s v    -     a  S c r e e n  S a V e r
  9. **
  10. **    Copyright (C) 1986 By Perry S. Kivolowitz
  11. **
  12. * The author grants permission for the non-commercial
  13. * distribution of this software provided that this
  14. * and other identifying information remains intact.
  15. **
  16. * This software is provided "as-is" and carries
  17. * with it no explicit or implicit promise of support
  18. * by the author. Nor will the author be held liable
  19. * for any damages, real or imagined, which may
  20. * result from the use or abuse of this software.
  21. **
  22. */
  23.  
  24. /*
  25. * This program is intended to be RUN from the
  26. * startup-sequence. It occupies a small amount of
  27. * memory and consumes very few cpu cycles. It will
  28. * attempt to detect some number of consecutive
  29. * minutes in which you haven't done anything at the
  30. * keyboard or mouse.
  31. **
  32. * After some minutes, ssv will bring a dark screen
  33. * to the front and do some eye magic designed not
  34. * to hurt your monitor. From time to time, ssv will
  35. * bring your original screen to the front again just
  36. * to remind you what's running.
  37. **
  38. * Clicking the left mouse button at any time
  39. * causes ssv to delete its screen and go back to
  40. * waiting for inactivity. If you want ssv to actually
  41. * EXIT - then click on the happy face which will
  42. * float around the screen.
  43. **
  44. */
  45.  
  46. extern void *OpenLibrary();
  47. extern struct Screen *OpenScreen();
  48. extern struct Window *OpenWindow();
  49. extern struct IntuiMessage *GetMsg();
  50. extern struct Task *FindTask();
  51.  
  52. #define RPPtr; struct RastPort *;
  53. #define BMPtr; struct BitMap *;
  54. #define IBPtr; struct IntuitionBase *;
  55. #define GMEM ((MEMF_CHIP) | (MEMF_CLEAR))
  56. #define NPTS 100
  57. #define HAPPY_FACE 999
  58. #define FACE_WIDTH 37L
  59. #define FACE_HEIGHT 21L
  60. #undef min
  61. #define min(a , b)((a) < (b) ? (a) : (b))
  62. static char *Author   = "Perry S. Kivolowitz";
  63. static char *ILibrary = "intuition.library";
  64. static char *GLibrary = "graphics.library";
  65. struct IntuitionBase *IntuitionBase;
  66. struct GfxBase       *GfxBase;
  67. struct Window        *w = NULL;
  68. struct Screen        *s = NULL;
  69. struct ViewPort     *vp = NULL;
  70. struct RastPort     *rp = NULL;
  71. static struct BitMap    fbm;
  72. static int sleepy_time = 4 * 12;
  73.  
  74. struct point {
  75. long x;
  76. long y;
  77. long dx;
  78. long dy;
  79. };
  80.  
  81. struct Gadget close_gadget = {
  82. NULL , 0 , 0 , FACE_WIDTH , FACE_HEIGHT , GADGHNONE |
  83. GADGIMAGE , GADGIMMEDIATE , BOOLGADGET , NULL , NULL , NULL ,
  84. 0 , NULL , HAPPY_FACE , NULL
  85. };
  86.  
  87. struct NewScreen     ns = {
  88. 0 , 0 , 640 , 200 , 1 , 0 , 0 , HIRES , CUSTOMSCREEN
  89. , NULL , NULL , NULL , NULL
  90. };
  91.  
  92. struct NewWindow     nw = {
  93. 0 , 0 , 640 , 200 , 0 , 0 , MOUSEBUTTONS | GADGETDOWN |
  94. INACTIVEWINDOW , NOCAREREFRESH | ACTIVATE | BORDERLESS |
  95. BACKDROP , &close_gadget , NULL , NULL , NULL , NULL , 0 , 0 ,
  96. 0 , 0 , CUSTOMSCREEN
  97. };
  98.  
  99. /* produced by GI from a DPaint brush file */
  100. unsigned short happy_face[63] = {   0x0000,0x0000,0x0000,
  101.  0x000f,0xff80,0x0000,  0x00f8,0x00f8,0x0000,
  102.  0x0387,0xff0e,0x0000,  0x0e7f,0xfff3,0x8000,
  103.  0x19ff,0xfffc,0xc000,  0x37ff,0xffff,0x6000,
  104.  0x2fe3,0xfe3f,0xa000,  0x6fdf,0xffdf,0xb000,
  105.  0x5fbb,0xfeef,0xd000,  0x5fff,0xdfff,0xd000,
  106.  0x5fff,0xdfff,0xd000,  0x6fff,0xefff,0xb000,
  107.  0x2fbf,0x8fef,0xa000,  0x37df,0xffdf,0x6000,
  108.  0x19e3,0xfe3c,0xc000,  0x0e7c,0x01f3,0x8000,
  109.  0x0387,0xff0e,0x0000,  0x00f8,0x00f8,0x0000,
  110.  0x000f,0xff80,0x0000,  0x0000,0x0000,0x0000
  111. };
  112.  
  113. main(argc , argv)
  114. char *argv[];
  115. {
  116.    int result = 0;
  117.  
  118.    if (argc > 1) sleepy_time = atoi (argv[1]) * 12;
  119.    if (sleepy_time <= 0) sleepy_time = 6;
  120.    IntuitionBase = (struct IntuitionBase *) OpenLibrary(ILibrary , 0L);
  121.    GfxBase = (struct GfxBase *) OpenLibrary(GLibrary , 0L);
  122.    init_happy_face();
  123.    if (IntuitionBase && GfxBase)  {
  124.       while (result != HAPPY_FACE)  {
  125.          wait_for_inactivity();
  126.          if (!(nw.Screen = s = OpenScreen(&ns))  ||
  127.             !(w = OpenWindow(&nw))) goto out;
  128.             ShowTitle(s , 0L);
  129.             set_pointers();
  130.             set_color_map();
  131.             while (1)  {
  132.                ScreenToFront(s);
  133.                if (result = zoom()) break;
  134.                ScreenToBack(s);
  135.                Delay(300L);
  136.             }
  137.          CloseWindow(w);
  138.          CloseScreen(s);
  139.       }
  140.    }
  141.    out:if (GfxBase) CloseLibrary(GfxBase);
  142.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  143. }
  144.  
  145. set_pointers()
  146. {  vp = &w->WScreen->ViewPort;
  147.    rp = &s->RastPort;
  148. }
  149. set_color_map()
  150. {
  151.    SetRGB4(vp , 0L , 0L , 0L , 0L);
  152.    SetRGB4(vp , 1L , 0L , 0L , 0L);
  153.    SetRast(rp , 0L);
  154.    SetDrMd(rp , COMPLEMENT);
  155.    SetAPen(rp , 1L);
  156. }
  157.  
  158. zoom()
  159. {
  160.    unsigned long class;
  161.    register struct point *p;
  162.    struct point array[NPTS] , face_position;
  163.    register struct point *end_point = &array[NPTS];
  164.    long red , green , blue;
  165.    int style , loops;
  166.    struct IntuiMessage *message;
  167.  
  168.    set_dots(array);
  169.    set_face(&face_position);
  170.    red = rand(9) + 5; green = rand(8) + 5;
  171.    blue = rand(8) + 5;
  172.    ramp_up(red , green , blue);
  173.    for (style = 0; style < 2; style++)  {
  174.       loops = style == 0 ? 3500 : 2500;
  175.       while (--loops)  {
  176.          if (message = GetMsg(w->UserPort))  {
  177.             class = message->Class;
  178.             ReplyMsg(message);
  179.             break;
  180.          }
  181.          p = array + (loops & 3);
  182.          if ((loops & 7) == 0) jerk_pointer(loops);
  183.          WaitBOVP(vp);
  184.          move_dots(0 , style , &face_position);
  185.          while (p < end_point)  {
  186.             move_dots(1 , style , p);
  187.             p += 4;
  188.          }
  189.       }
  190.    if (message) break;
  191.    }
  192.    ramp_down(red , green , blue);
  193.    SetRast(rp , 0L);
  194.    if (message == NULL) return(0);
  195.    return(class == GADGETDOWN ? HAPPY_FACE : 1);
  196. }
  197.  
  198. /*
  199. * If you are not using MANX C or are using a version
  200. * of MANX which does not have RangeRand (undocumented
  201. * library call), simply replace this routine with one
  202. * that returns a random number between 0 and x - 1.
  203. */
  204.  
  205. rand(X)
  206. {
  207.    long seconds , microseconds;
  208.  
  209.    CurrentTime(&seconds , µseconds);
  210.    return((RangeRand((short) (1 << 15)) + 0xFFF & microseconds) % X);
  211. }
  212.  
  213. ramp_up(red , green , blue)
  214. register long red , green , blue;
  215. {
  216.    register long i;
  217.  
  218.    for (i = 0; i < 16; i++)  {
  219.       Delay(5L);
  220.       SetRGB4(vp , 1L , min(i,red) , min(i,green) , min(i,blue));
  221.    }
  222. }
  223.  
  224. ramp_down(red , green , blue)
  225. register red , green , blue;
  226. {
  227.    register long i;
  228.  
  229.    for (i = 15; i >= 0; i--)  {
  230.       Delay(5L);
  231.       SetRGB4(vp , 1L , min(i,red) , min(i,green) , min(i,blue));
  232.    }
  233. }
  234.  
  235. set_face(p)
  236. register struct point *p;
  237. {
  238.  
  239.    close_gadget.LeftEdge = p->x = rand(580) + 20;
  240.    close_gadget.TopEdge  = p->y = rand(140) +20;
  241.    p->dx = 2;
  242.    p->dy = 1;
  243.    BltBitMap(&fbm , 0L , 0L , w->RPort->BitMap , p->x , p->y,
  244.             FACE_WIDTH , FACE_HEIGHT , 0x60L , 0xFFL , NULL);
  245. }
  246.  
  247. set_dots(array)
  248. struct point array[];
  249. {
  250.    register struct point *p;
  251.    register long i;
  252.  
  253.    for (i = 0, p = array; i < NPTS; i++ , p++)  {
  254.       do  {
  255.          p->x = rand(620) + 10;
  256.          p->y = rand(180) + 10;
  257.       }  while (ReadPixel(rp , p->x , p->y) == 1);
  258.       while (!(p->dx = rand(13) - 6));
  259.       while (!(p->dy = rand(13) - 6));
  260.       WritePixel(rp , p->x , p->y);
  261.    }
  262. }
  263.  
  264. move_dots(flag , style , p)
  265. register int flag , style;
  266. register struct point *p;
  267. {
  268.    register long oldx , oldy;
  269.  
  270.    oldx = p->x;
  271.    oldy = p->y;
  272.    p->x += p->dx;
  273.    p->y += p->dy;
  274.    if ((p->x >= (flag ? 640 : 640 - FACE_WIDTH)) || (p->x < 0))  {
  275.       p->x -= p->dx;
  276.       p->dx = -p->dx;
  277.    }
  278.    if ((p->y >= (flag ? 200 : 200 - FACE_HEIGHT)) || (p->y <0 ))  {
  279.       p->y -= p->dy;
  280.       p->dy = -p->dy;
  281.    }
  282.    if (flag)  {
  283.       if (style == 0) WritePixel(rp , oldx , oldy);
  284.       WritePixel(rp , p->x , p->y);
  285.    }
  286.    else
  287.    {
  288.       BltBitMap(&fbm , 0L , 0L , w->RPort->BitMap , oldx , oldy,
  289.          FACE_WIDTH , FACE_HEIGHT , 0x60L , 0xFFL , NULL);
  290.       BltBitMap(&fbm , 0L , 0L , w->RPort->BitMap , p->x , p->y,
  291.          FACE_WIDTH , FACE_HEIGHT , 0x60L , 0xFFL , NULL);
  292.       close_gadget.TopEdge = p->y;
  293.       close_gadget.LeftEdge = p->x;
  294.    }
  295. }
  296.  
  297. jerk_pointer(counter)
  298. int counter;
  299. {
  300.    register long color_register = (counter % 3) + 17;
  301.    register long red , green , blue;
  302.  
  303.    red = rand(10); green = rand(10); blue = rand(10);
  304.    SetRGB4(vp , color_register , red , green , blue);
  305. }
  306.  
  307. init_happy_face()
  308. {
  309.    InitBitMap(&fbm , 1L , FACE_WIDTH , FACE_HEIGHT);
  310.    fbm.Planes[0] = (PLANEPTR) happy_face;
  311. }
  312.  
  313. long InputActivity()
  314. {
  315.    register struct Task *p = FindTask("input.device");
  316.    register long return_value = 0;
  317.  
  318. Forbid();
  319. if (p)  {
  320.    return_value = (long) *(p->tc_SPReg - 2);
  321. }
  322. Permit();
  323. return(return_value);
  324. }
  325.  
  326. long TotalMem()
  327. {
  328.    register unsigned long AvailMem();
  329.    register long availmem;
  330.  
  331.    Forbid();
  332.    availmem = AvailMem(MEMF_FAST);
  333.    availmem += AvailMem(MEMF_CHIP);
  334.    Permit();
  335.    return (availmem);
  336. }
  337.  
  338. wait_for_inactivity()
  339.  
  340. {
  341.    register long available_memory , last_available_memory;
  342.    register long repetitions = 0 , input_action , last_input_action;
  343.  
  344.    last_available_memory = TotalMem();
  345.    last_input_action = InputActivity();
  346.    while (repetitions < sleepy_time)  {
  347.       Delay(50L * 5L);
  348.       available_memory = TotalMem();
  349.       input_action = InputActivity();
  350.       if ((last_available_memory != available_memory) ||
  351.          (last_input_action != input_action))
  352.       {
  353.          last_available_memory = available_memory;
  354.          last_input_action = input_action;
  355.          repetitions = 0;
  356.       }
  357.       else repetitions++;
  358.    }
  359. }
  360.  
  361.  
  362.